package br.com.bancodados.repositorio;

import java.io.InputStreamReader;
import java.lang.reflect.Type;
import java.util.ArrayList;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import br.com.bancodados.exception.BusinessException;
import br.com.bancodados.model.Carro;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class RepositorioCarroWS implements IRepositorioCarro {

	private Gson mGson;
	private static RepositorioCarroWS instance;
	private final String WS_URL_POST = "http://150.161.125.62:8080/SmartServices/rest/carro/insert";
	private final String WS_URL_GET = "http://150.161.125.62:8080/SmartServices/rest/carro/getall";
	private final String WS_URL_DELETE = "http://150.161.125.62:8080/SmartServices/rest/carro/delete";
	private final String WS_URL_UPDATE = "http://150.161.125.62:8080/SmartServices/rest/carro/update";
	private final String WS_URL_GET_BY_MODEL = "http://150.161.125.62:8080/SmartServices/rest/carro/getbymodel";

	public static RepositorioCarroWS getInstance() {
		if (instance == null) {
			instance = new RepositorioCarroWS();
		}
		return instance;
	}

	private RepositorioCarroWS() {
		mGson = new Gson();
	}

	@Override
	public ArrayList<Carro> getAllCarros() throws BusinessException {

		try {
			ArrayList<Carro> carrosFromService = null;
			DefaultHttpClient client = new DefaultHttpClient();

			HttpGet httpget = new HttpGet(WS_URL_GET);
			HttpResponse response;

			response = client.execute(httpget);

			Type listType = new TypeToken<ArrayList<Carro>>() {
			}.getType();

			carrosFromService = mGson.fromJson(new InputStreamReader(response
					.getEntity().getContent()), listType);
			return carrosFromService;
		} catch (Exception ex) {
			throw new BusinessException();
		}

	}

	@Override
	public void cadastrarCarro(Carro carro) throws BusinessException {
		try {

			DefaultHttpClient client = new DefaultHttpClient();

			HttpPost httpPost = new HttpPost(WS_URL_POST);

			ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
			nameValuePairs.add(new BasicNameValuePair("carro", mGson
					.toJson(carro)));

			httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
			client.execute(httpPost);
		} catch (Exception ex) {
			throw new BusinessException();
		}

	}

	@Override
	public void atualizarCarro(Carro carro) throws BusinessException {
		try {

			DefaultHttpClient client = new DefaultHttpClient();

			HttpPost httpPost = new HttpPost(WS_URL_UPDATE);

			ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
			nameValuePairs.add(new BasicNameValuePair("carro", mGson
					.toJson(carro)));

			httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
			client.execute(httpPost);
		} catch (Exception ex) {
			throw new BusinessException();
		}

	}

	@Override
	public void deletarCarro(Carro carro) throws BusinessException {
		try {

			DefaultHttpClient client = new DefaultHttpClient();

			HttpPost httpPost = new HttpPost(WS_URL_DELETE);

			ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
			nameValuePairs.add(new BasicNameValuePair("carro", mGson
					.toJson(carro)));

			httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
			client.execute(httpPost);
		} catch (Exception ex) {
			throw new BusinessException();
		}

	}

	@Override
	public ArrayList<Carro> getCarroPorModelo(String modelo)
			throws BusinessException {
		try {
			ArrayList<Carro> carrosFromService = null;
			DefaultHttpClient client = new DefaultHttpClient();

			HttpGet httpget = new HttpGet(WS_URL_GET_BY_MODEL);
			HttpResponse response;

			response = client.execute(httpget);

			Type listType = new TypeToken<ArrayList<Carro>>() {
			}.getType();

			carrosFromService = mGson.fromJson(new InputStreamReader(response
					.getEntity().getContent()), listType);
			return carrosFromService;
		} catch (Exception ex) {
			throw new BusinessException();
		}

	}

}
